/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webanovare;

import com.webanovare.model.NewsRegistry;
import com.webanovare.model.db.News;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author Richard
 */
@Stateless
@Path("/news")
public class NewsResource {
    @EJB
    NewsRegistry reg;    
    
    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void add(@FormParam("text") String newsText) {

        System.out.println("add" + reg);
        reg.add(new News(newsText));
        System.out.println("efter add");
    }
    
    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Long id) {
        System.out.println("remove" + reg);
        reg.removeWithId(id);
        System.out.println("efter remove");
    }
    
    @PUT
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public void update(@FormParam("id") Long id, @FormParam("text") String newsText) {
        System.out.println("update");
        reg.update(new News(id, newsText));
        System.out.println("efter update");
    }
}
